package it.unina.lab.config;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.log4j.Logger;

import com.continuent.tungsten.commons.patterns.fsm.Action;
import com.continuent.tungsten.commons.patterns.fsm.Entity;
import com.continuent.tungsten.commons.patterns.fsm.EntityAdapter;
import com.continuent.tungsten.commons.patterns.fsm.Event;
import com.continuent.tungsten.commons.patterns.fsm.Transition;
import com.continuent.tungsten.commons.patterns.fsm.TransitionFailureException;
import com.continuent.tungsten.commons.patterns.fsm.TransitionRollbackException;

public class ActionDispatcher implements Action {

	
	private Logger logger = Logger.getLogger(ActionDispatcher.class);
	private String actionToDo;
	
	public ActionDispatcher(String actionToDo){
		this.actionToDo = actionToDo;
	}
	
	@Override
	public void doAction(Event message, Entity entity, Transition transition,
			int actionType) throws TransitionRollbackException,
			TransitionFailureException {
		
		logger.info("ACTION DISPACHER CALL METHODS: " + actionToDo);
		EntityAdapter adapter = (EntityAdapter) entity;
		MyEntityTest my = (MyEntityTest) adapter.getEntity();
		
		
		Class<?> classe;
		try {
			classe = Class.forName("it.unina.lab.config.MyEntityTest");
			Method metodo = classe.getMethod(actionToDo, null);
			metodo.invoke(my, null);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//my.setProva(actionToDo);
		//System.out.println(my);
	}

}


/*
Object myObject = null;
try {
	Class<?> classe = Class.forName("it.unina.lab.config.MusicPlayerActions");
	Constructor<?> costruttore = classe.getConstructor(String.class);
	myObject = costruttore.newInstance(action_value);
} catch (ClassNotFoundException e1) {
	// TODO Auto-generated catch block
	e1.printStackTrace();
} catch (SecurityException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (NoSuchMethodException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IllegalArgumentException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (InstantiationException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IllegalAccessException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (InvocationTargetException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

*/